from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from dhm.models.chating.chating import Chating

class ChatingView(APIView):
    permission_calsses = ([IsAuthenticated])
    def get(self, request):
        photo = request.GET.get('photo', '')
        content = request.GET.get('content', '')
        user_id = int(request.GET.get('user_id', 1))
        chating_time = request.GET.get('chating_time', '')
        user_photo = request.GET.get('user_photo', '')
        user_username = request.GET.get('user_username', '')
        other_id = int(request.GET.get('other_id', 1))
        chatings = Chating.objects.filter(user_id=user_id, other_id=other_id).order_by('chating_time')
        data = []

        for chating in chatings:
            data.append({
                'id': chating.id,
                'photo': chating.photo,
                'content': chating.content,
                'user_id': chating.user_id,
                'chating_time': chating.chating_time,
                'user_photo': chating.user_photo,
                'user_username': chating.user_username,
                'other_id': other_id,
            })
        return Response(data)

    def post(self, request):
        data = request.POST
        # photo = data.get('photo', '')
        content = data.get('content', '')
        user_id = int(data.get('user_id', 1))
        chating_time = data.get('chating_time', '')
        user_photo = data.get('user_photo', '')
        user_username = data.get('user_username', '')
        other_id = data.get('other_id', '')
        # username = data.get('username', '')
        Chating.objects.create(content=content, user_id=user_id, chating_time=chating_time, user_photo=user_photo, user_username=user_username, other_id=other_id)
        return Response({
            'result': "success"
        })
